Partial Class Admin_Add_Cities
  Inherits System.Web.UI.Page

    Private Sub SecurePage()
        Dim sec As New Secure

        sec.ParentPage = Me
        sec.AddDataGridColumn(Me.DataGrid, DataGrid.Columns.Count - 2, Secure.ControlTypes.DeleteType)
        sec.AddDataGridColumn(Me.DataGrid, DataGrid.Columns.Count - 3, Secure.ControlTypes.EditType)
        sec.SetPageAccess(Session("UserName"))
        sec = Nothing
    End Sub

  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    If country_cmb.Items.Count < 1 Then
      Dim zClass As New LookUp.General_Lookup
      zClass.LookUpCode = "CITIES"
      DataGrid.CurrentPageIndex = 0

      zClass.Loader(country_cmb, "COUNTRY", "country_desc", "country_cd")
      country_cmb.Items.Insert(0, "")
      If Session("country_cd") <> Nothing Then
        Back_Panel.Visible = True
        country_cmb.Items.FindByValue(Session("country_cd")).Selected = True
        zClass.DataGridFill(DataGrid, Results_lbl, Session("country_cd"))
      Else
        zClass.DataGridFill(DataGrid, Results_lbl)
      End If

    End If

    Dim Edit_Image As Image = CType(Me.Master.FindControl("L3"), Image)
    Edit_Image.ImageUrl = "~/images/redT.PNG"
    Dim Edit_Button As LinkButton = CType(Me.Master.FindControl("Cities_btn"), LinkButton)
    Edit_Button.ForeColor = Drawing.Color.Black
    Dim Edit_Panel As Panel = CType(Me.Master.FindControl("Lookup_Panel"), Panel)
    Edit_Panel.Visible = True
        SecurePage()
  End Sub

  Protected Sub Add_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Add_btn.Click
    OnMode("Add") : ViewState("mode") = "Add"
    Name_txt.Text = ""
    Name_txt.Focus()
  End Sub

  Private Sub OnMode(ByVal mode As String)

    If mode = "Edit" Then
      country_cmb.Enabled = False
    End If

    DataGrid.Enabled = False
    Add_btn.Enabled = False
    AddEdit_Panel.Visible = True

  End Sub

  Private Sub NotOnMode()
    country_cmb.Enabled = True
    DataGrid.Enabled = True
    Add_btn.Enabled = True
    AddEdit_Panel.Visible = False

  End Sub

  Protected Sub Cancel_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Cancel_btn.Click
    NotOnMode()
    ViewState.Remove("mode")
  End Sub

  Protected Sub Save_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Save_btn.Click
        Dim zClass As New LookUp.General_Lookup
        Dim dm As New DAOMain.CommonDAO

        If ViewState("mode") = "Add" Then
            zClass.LookUpCode = "CITIES"
            zClass.ExecQuery("INSERT INTO country_city_dtl(city_cd, country_cd, city_desc, " & _
                "city_crea_by, city_crea_dt) VALUES('" & Trim(zClass.MaxCode()) & "','" & _
                Trim(country_cmb.SelectedValue.ToString) & "'," & UCase(dm.Q(Trim(Name_txt.Text))) & _
                ",'" & UCase(Session("UserName")) & "','" & Now & "')")

        ElseIf ViewState("mode") = "Edit" Then
            zClass.ExecQuery("UPDATE country_city_dtl SET " & _
                "city_desc=" & UCase(dm.Q(Trim(Name_txt.Text))) & ", " & _
                "city_last_upd_by='" & UCase(Session("UserName")) & "', " & _
                "city_last_upd_dt='" & Now & "' " & _
                "WHERE city_cd='" & Trim(ViewState("city_cd").ToString) & "'")

        End If

        zClass.LookUpCode = "CITIES"
        DataGrid.CurrentPageIndex = 0
        zClass.DataGridFill(DataGrid, Results_lbl, Trim(country_cmb.SelectedValue.ToString))
        NotOnMode()
        ViewState.Remove("mode")
        ViewState.Remove("city_cd")
  End Sub

  Protected Sub DataGrid_DeleteCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid.DeleteCommand
    Dim zClass As New LookUp.General_Lookup
    Dim dm As New DAOMain.CommonDAO
    zClass.LookUpCode = "CITIES"

    If zClass.OKtoDELETE(Trim(e.Item.Cells(0).Text)) = False Then Exit Sub

        zClass.ExecQuery("DELETE FROM country_city_dtl WHERE city_cd=" & dm.Q(Trim(e.Item.Cells(0).Text)) & _
          " AND country_cd=" & dm.Q(Trim(e.Item.Cells(4).Text)))

    DataGrid.CurrentPageIndex = 0
    zClass.DataGridFill(DataGrid, Results_lbl, Trim(country_cmb.SelectedValue.ToString))

  End Sub

  Protected Sub DataGrid_EditCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid.EditCommand
    ViewState("mode") = "Edit"
    Name_txt.Text = e.Item.Cells(1).Text
    ViewState("city_cd") = e.Item.Cells(0).Text
    country_cmb.SelectedItem.Selected = False
    country_cmb.Items.FindByValue(e.Item.Cells(4).Text).Selected = True
    OnMode("Edit")
    Name_txt.Focus()

  End Sub

  Protected Sub Back_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Back_btn.Click
    Session.Remove("country_cd")
    Response.Redirect("~/Admin/Countries.aspx")
  End Sub

    Protected Sub country_cmb_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles country_cmb.SelectedIndexChanged
        Dim zClass As New LookUp.General_Lookup

        zClass.LookUpCode = "CITIES"
        DataGrid.CurrentPageIndex = 0
        zClass.DataGridFill(DataGrid, Results_lbl, country_cmb.SelectedValue.ToString)
        zClass = Nothing
    End Sub

    Protected Sub DataGrid_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles DataGrid.PageIndexChanged
        Dim zClass As New LookUp.General_Lookup

        If e.NewPageIndex <= DataGrid.PageCount Then
            zClass.LookUpCode = "CITIES"
            DataGrid.CurrentPageIndex = e.NewPageIndex
            zClass.DataGridFill(DataGrid, Results_lbl, country_cmb.SelectedValue.ToString)
        End If

        zClass = Nothing
    End Sub


End Class